package com.kun.example.rpc;

import com.kun.example.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author 小K
 * @date 2022/4/9 0009 - 下午 15:51
 */
public class RemoteInvocationHandler implements InvocationHandler {

    private String host;
    private int port;

    public RemoteInvocationHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //远程连接
        RpcNetTransport transport = new RpcNetTransport(host, port);
        transport.newSocket();

        //调用接口，方法，方法参数
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setArgs(args);
        rpcRequest.setClassName(method.getDeclaringClass().getName());
        rpcRequest.setTypes(method.getParameterTypes());
        rpcRequest.setMethodName(method.getName());
        return transport.send(rpcRequest);

    }
}
